package ace.tool.generate.code.impl.resolver.scheme.resolver.sdk;

import ace.cmp.util.CaseFormatUtils;
import ace.tool.generate.code.impl.manager.common.EntityModelManager;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.sdk.FeignClassInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.ClassFileInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.CommonInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.ProjectInfo;
import ace.tool.generate.code.impl.resolver.scheme.resolver.ClassFileInfoResolver;
import ace.tool.generate.code.impl.service.model.input.GenerateInput;
import ace.tool.generate.code.impl.service.model.input.GenerateModelInput;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import org.apache.commons.text.WordUtils;

/**
 * @author caspar
 * @date 2023/11/17 10:34
 */
@Builder
@RequiredArgsConstructor
public class FeignClassInfoResolver {

  private final GenerateInput input;
  private final GenerateModelInput modelInput;
  private final CommonInfo commonInfo;
  private final ProjectInfo sdkProjectInfo;

  public FeignClassInfo resolve() {
    String entityModelId = new EntityModelManager().getEntityModelId(input, modelInput);

    ClassFileInfo classFileInfo =
        ClassFileInfoResolver.builder()
            .className(this.modelInput.getName())
            .classNameSuffix(input.getFeignClassNameSuffix())
            .classPackagePrefix(input.getFeignClassPackagePrefix())
            .rootPackageFullName(commonInfo.getRootPackageFullName())
            .sourceCodeRootDir(sdkProjectInfo.getSourceCodeRootDir())
            .build()
            .resolve();

    return FeignClassInfo.builder()
        .entityModelId(entityModelId)
        .info(classFileInfo)
        .restNamespace(input.getProjectName())
        .entityClassNameLowercaseDashName(this.getEntityClassNameLowercaseDashName())
        .feignClientConfigPrefix(this.getFeignClientConfigPrefix())
        .feignFirstLowercaseClassName(this.getFeignFirstLowercaseClassName(classFileInfo))
        .build();
  }

  private String getFeignFirstLowercaseClassName(ClassFileInfo classFileInfo) {
    return WordUtils.uncapitalize(classFileInfo.getClassName());
  }

  private String getFeignClientConfigPrefix() {
    return input.getFeignClientConfigPrefix();
  }

  private String getEntityClassNameLowercaseDashName() {
    return CaseFormatUtils.convertUpperCamelToLowerUnderscore(modelInput.getName()).replaceAll("_", "-");
  }
}
